<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="baidu-site-verification" content="code-UjJKh3jtOo">
  <link rel="stylesheet" href="//unpkg.com/@highlightjs/cdn-assets@11.5.0/styles/atom-one-dark.min.css">
  <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
  <title>Tit1e</title>
  <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2767111597631391" crossorigin="anonymous"></script>
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="Tit1e" type="application/atom+xml">
</head>

<body class="dark">
    <div class="container">
    <header class="header">
  <div class="title">
      <a href="/" class="logo">Tit1e</a>
      <div class="btn-dark"></div>
      <script>
        let bodyClx = document.body.classList;
        let btnDark = document.querySelector('.btn-dark');
        let sysDark = window.matchMedia('(prefers-color-scheme: dark)');
        let darkVal = localStorage.getItem('dark');

        let setDark = (isDark) => {
        bodyClx[isDark ? 'add' : 'remove']('dark');
        localStorage.setItem('dark', isDark ? 'yes' : 'no');
        };

        setDark(darkVal ? darkVal === 'yes' : sysDark.matches);
        requestAnimationFrame(() => bodyClx.remove('not-ready'));

        btnDark.addEventListener('click', () => setDark(!bodyClx.contains('dark')));
        sysDark.addEventListener('change', (event) => setDark(event.matches));
      </script>
  </div>
    <ul class="menu">
        
        
        
        
        <li class="menu-item ">
            <a href="/" class="menu-item-link">Home</a>
        </li>
        
        
        <li class="menu-item ">
            <a href="/archives/" class="menu-item-link">Archives</a>
        </li>
        
        
        <li class="menu-item ">
            <a href="/atom.xml" class="menu-item-link">RSS</a>
        </li>
        
        
        <li class="menu-item ">
            <a target="_blank" rel="noopener" href="https://pixeltimer.art" class="menu-item-link">Pixel Timer</a>
        </li>
        
    </ul>
    <div class="search-box">
        <input type="search" id="search" placeholder="搜索">
    </div>
</header>
    <article class="post">
  <div class="post-meta">
    <span class="post-time">12 / 26, 2017</span>
  </div>
  <div class="post-title">
    <h1 class="title">vuex的简单使用教程</h1>
  </div>
  
  <ul class="post-tags">
    
    <li class="tag-item">
      #学习
    </li>
    
    <li class="tag-item">
      #vue
    </li>
    
  </ul>
  
  <div class="post-content">
    <p>公司数据平台现在的规模已经有点大了，考虑到以后，可能会出现数据共享的问题，所以我先粗略了解一下<code>vuex</code>以备不时之需。<br>首先安装<code>vuex</code></p>
<pre><code class="bash">#生产环境中也需要使用
npm install vuex --save
</code></pre>
<span id="more"></span>
<p><code>src</code>目录下新建<code>vuex</code>目录</p>
<p>vuex文件夹下新建<code>store.js</code><br>先在<code>store.js</code>中写入</p>
<pre><code class="javascript">import Vue from &#39;vue&#39;
import Vuex from &#39;vuex&#39;
Vue.use(Vuex)
//state用于存储数据
const state = &#123;
  count: 10
&#125;
//mutations用于声明方法
const mutations = &#123;
  add(state,n) &#123;
    state.count += n
  &#125;,
  reduce(state,n) &#123;
    state.count -= n
  &#125;
&#125;
//getters用于监听，比如数据变化时执行某个函数
const getters = &#123;
    //count变化时就会执行
    count: state =&gt; &#123;
        return console.log(state.count)
    &#125;
&#125;
//actions类似于mutations，但actions可以异步执行
const actions = &#123;
  addAction(&#123; commit &#125;) &#123;
    commit(&#39;add&#39;,1)
  &#125;,
  reduceAction(&#123; commit &#125;) &#123;
    commit(&#39;reduce&#39;)
  &#125;
&#125;
//暴露这些对象
export default new Vuex.Store(&#123;
  state,
  mutations,
  getters,
  actions
&#125;)
</code></pre>
<p>组件中使用</p>
<pre><code class="html">&lt;tempate&gt;
    &lt;div&gt;
        &#123;&#123; count &#125;&#125;
        &lt;button @click=&quot;add(10)&quot;&gt;-&lt;/button&gt;
        &lt;button @click=&quot;reduce(10)&quot;&gt;+&lt;/button&gt;
    &lt;/div&gt;
&lt;/template&gt;
</code></pre>
<pre><code class="javascript">//引入
import store from &#39;../vuex/store&#39;
//使用这种方式引入后，可以像平时一样使用store从的值与方法，其实还有另外两种调用方法，但我个人喜欢这种，这也是最简单的写法，所以另外两种我就不赘述了。
import &#123; mapState,mapMutations,mapGetters,mapActions &#125; from &#39;vuex&#39;
export default &#123;
    store,
    computed: &#123;
        ...mapState([&#39;count&#39;]),
        ...mapGetters([&#39;count&#39;])
      &#125;,
      methods:&#123;
        ...mapMutations([&#39;add&#39;,&#39;reduce&#39;]),
        ...mapActions([&#39;addAction&#39;,&#39;reduceAction&#39;])
      &#125;
&#125;
</code></pre>

  </div>
  <div class="post-near">
    
    <a
      class="post-near-prev"
      href="/p/890d484c.html"
      title="你好呀，2018"
    >
      <span class="arrow">←</span>
      <span class="post-near-title">
        你好呀，2018
      </span>
    </a>
    
    
    <a
      class="post-near-next"
      href="/p/9501c48.html"
      title="局域网请求 webpack 反向代理"
    >
      <span class="post-near-title">
        局域网请求 webpack 反向代理
      </span>
      <span class="arrow">→</span>
    </a>
    
  </div>
</article>

    <footer class="footer">
  <p>本博客采用 <a class="license-link" rel="license noopener" target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">署名-非商业性使用-相同方式共享 4.0 国际</a>协议进行许可</p>
  <p>
    <span>Thanks For <a target="_blank" rel="noopener" href="https://github.com/nanxiaobei/hugo-paper" taget="_blank">hugo-paper</a></span>
    <span style="margin-left: 10px;">&copy; 2016-2025 by Tit1e</span>
  </p>
</footer>
    </div>
    <script src="//unpkg.com/@highlightjs/cdn-assets@11.5.0/highlight.min.js"></script>
<script>
  hljs.initHighlightingOnLoad();
  var _hmt = _hmt || [];
    (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?6349d3bc54baea0b04a65145c5e3b799";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
    })();
    const block = document.querySelectorAll('.post-content')[0]
    block.addEventListener('click', e => {
        const {nodeName, src} = e.target
        if(nodeName !== 'IMG') return
        document.body.classList.add('overflow-hidden')
        const div = document.createElement('div')
        div.id = 'global-cover'
        div.title = '点击关闭'
        const a = document.createElement('a')
        a.href = src
        a.target="_blank"
        a.title = '点击打开原图'
        const img = document.createElement('img')
        img.className = 'show-pic'
        img.src = src
        a.appendChild(img)
        div.appendChild(a)
        document.body.appendChild(div)
    })
    document.body.addEventListener('click', e => {
        if(e.target.id === 'global-cover'){
            document.body.classList.remove('overflow-hidden')
            document.body.removeChild(e.target)
        }
    })
</script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-7M04VN3JZ4"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-7M04VN3JZ4');
</script>

<script src="/js/search.js"></script>


</body>
</html>